package cn.wk.libs4a.utils;

import android.content.Context;
import android.os.Handler;
import cn.wk.libs4a.WKApplication;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * 百度地图定位工具类
 */
public class WKBDLocationUtils implements BDLocationListener {

	public Context ctx;

	private BDLocation data;

	/**
	 * 定位信息
	 */
	public String city_name = "威海";// 城市
	public String city_id = "1258";// 城市ID

	/**
	 * 定位返回
	 */
	private Handler handler;

	/**
	 * 定位相关
	 */
	public LocationClient mLocClient;

	/**
	 * 是否获取完立马停止
	 */
	private boolean startOnce = true;

	public WKBDLocationUtils(Context ctx) {
		this.ctx = ctx;
		init();
	}

	public WKApplication app() {
		return (WKApplication) this.ctx.getApplicationContext();
	}

	/**
	 * 从本地初始化城市信息
	 */
	public void initCityFromLocal() {
		city_name = app().sp.getString("CITY_NAME");
		if (city_name == null || city_name.equals("")) {
			city_name = "威海市";
		}
		city_id = app().sp.getString("CITY_ID");
		if (city_id == null || city_id.equals("")) {
			city_id = "1258";
		}
	}

	/**
	 * 保存城市信息
	 */
	public void saveCity(String city_name, String city_id) {
		this.city_name = city_name;
		this.city_id = city_id;
		app().sp.setString("CITY_NAME", city_name);
		app().sp.setString("CITY_ID", city_id);
	}

	/**
	 * 开始获取
	 */
	public void start() {
		testForEmulator();
		mLocClient.start();
	}

	/**
	 * 开始获取,获取到就停止
	 */
	public void startOnce() {
		testForEmulator();
		startOnce = true;
		mLocClient.start();
	}

	/**
	 * 开始获取,获取到就停止
	 */
	public void startOnce(Handler handler) {
		testForEmulator();
		startOnce = true;
		mLocClient.start();
		this.handler = handler;
	}

	/**
	 * 停止
	 */
	public void stop() {
		mLocClient.stop();
	}

	/**
	 * 初始化地理位置
	 */
	public void init() {
		mLocClient = new LocationClient(ctx);
		mLocClient.registerLocationListener(this);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setIsNeedAddress(true);
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);

		initCityFromLocal();
	}

	/**
	 * 定位SDK监听函数
	 */
	public void onReceiveLocation(BDLocation location) {
		if (location == null) {
			if (handler != null) {
				handler.sendEmptyMessage(0);
			}
			return;
		}
		// 获取到数据就停止
		if (startOnce)
			this.stop();
		this.data = location;

		if (handler != null) {
			handler.sendEmptyMessage(1);
		}
		// String str = "定位：" + location.getAddrStr() + " city="
		// + location.getCity() + "-" + location.getCityCode();
		// WKApplication.showToast("" + str);
	}

	/**
	 * 定位SDK监听函数
	 */
	public void onReceivePoi(BDLocation poiLocation) {
	}

	/**
	 * 销毁方法，在app里添加
	 */
	public void onDestroy() {
		// 退出时销毁定位
		if (mLocClient != null)
			mLocClient.stop();
	}

	/**
	 * 模拟器测试方法
	 */
	public boolean testForEmulator() {
		if (WKUtils.isEmulator(ctx)) {
			saveCity("威海市", "1258");
			return true;
		}
		return false;
	}

	/**
	 * 纬度
	 * 
	 * @return
	 */
	public double lat() {
		if (data == null) {
			return 0;
		}
		return data.getLatitude();
	}

	/**
	 * 经度
	 * 
	 * @return
	 */
	public double lng() {
		if (data == null) {
			return 0;
		}
		return data.getLongitude();
	}

	/**
	 * 城市
	 */
	public String getCity() {
		if (data == null)
			return "";
		return data.getCity();

	}
}
